<?php
/**
 * Description of Emms_Cron_ActiveLoans
 *
 * @author andres
 */
class Emms_Cron_ActiveLoans extends Emms_Cron implements Emms_Interface_Cron
{
    /*
     * Update active_loans with delay and penalties acumulated on each active loan.
     * Note that active_loans only deals with the next expected payment for each 
     * active loan, for loans with more than 1 due payment, only the older due 
     * payment values are considered.
     */
    public function update()
    {
        try {
            $this->db->query(sprintf("
                update active_loans al, loans l
                set
                    al.CurrentDate = '%s',
                    al.CurrentDelay = GREATEST(0,(TO_DAYS('%s')-TO_DAYS(NextPaymentDate))),
                    al.CurrentPenalties = IF(al.CurrentDelay <= l.DelayGrace, 0, 
                    l.DelayPenaltyFixedCharge+0.01*l.DelayPenaltyRate*GREATEST(0,(TO_DAYS('%s')-TO_DAYS(al.NextPaymentDate)))*al.ExpectedPaymentAmount)
                where
                    l.LoanId = al.LoanId",$this->date,$this->date,$this->date));
        } catch (Exception $e) {
            throw $e;
        }
    }
}
